#!/bin/sh

if [ "$ACTION" = "ifup" -a "$INTERFACE" = "lan" ] ; then
	lan_ip=$(ifstatus lan | jsonfilter -e '@["ipv4-address"][0].address')

	lan_ip_old="$(grep -Eo '(^[^0-9]|\b)((1[0-9]{2}|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])\.){3}(1[0-9][0-9]|2[0-4][0-9]|25[0-5]|[1-9][0-9]|[0-9])([^0-9]|\b) '  /tmp/dnsmasq-lede.router | sed -nr 's/([^0-9]|\b)(([0-9]{1,3}\.){3}[0-9]{1,3})([^0-9]|\b)/\2/p')"

	if [ -n "$lan_ip" -a "$lan_ip" !=  "$lan_ip_old" ] ; then
		echo "$lan_ip lede.router" > /tmp/dnsmasq-lede.router
		/etc/init.d/dnsmasq restart
	fi
fi
